package com.ruoyi.system.service.impl;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.HexUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.config.oss.OssProperties;
import com.ruoyi.common.config.oss.OssTemplate;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.vo.PreventionCheckTaskConfigVO;
import com.ruoyi.system.entity.PreventionCheckTask;
import com.ruoyi.system.entity.PreventionUnit;
import com.ruoyi.system.mapper.PreventionCheckTaskConfigMapper;
import com.ruoyi.system.mapper.PreventionUnitMapper;
import com.ruoyi.system.service.PreventionUnitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.List;

/**
 * (PreventionUnit)表服务实现类
 * @since 2022-12-02 16:53:56
 */
@Service("preventionUnitService")
public class PreventionUnitServiceImpl implements PreventionUnitService {
    @Resource
    private PreventionUnitMapper preventionUnitMapper;

    @Autowired
    private OssTemplate template;

    @Autowired
    private PreventionCheckTaskConfigMapper preventionCheckTaskConfigMapper;

    @Autowired
    private OssProperties ossProperties;
    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public PreventionUnit queryById(Integer id) {
        return this.preventionUnitMapper.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param preventionUnit 筛选条件
     * @return 查询结果
     */
    @Override
    public List<PreventionUnit> queryByPage(PreventionUnit preventionUnit) {
        return this.preventionUnitMapper.queryAllByLimit(preventionUnit);
    }

    /**
     * 新增数据
     *
     * @param preventionUnit 实例对象
     * @return 实例对象
     */
    @Override
    public PreventionUnit insert(PreventionUnit preventionUnit) {
        this.preventionUnitMapper.insert(preventionUnit);
        String id = preventionUnit.getId().toString();

        JSONObject object = new JSONObject();
        object.put("id",id);
        object.put("type", Constants.PR);
        String json = JSONUtil.toJsonStr(object);

        File file = new File(id + "-prevention.jpg");
        File generate = QrCodeUtil.generate(json, 300, 300, file);
        InputStream in = null;
        try {
            in = new FileInputStream(generate);
            template.putObject(ossProperties.getBucketName(),id + "-prevention.jpg",in);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            IoUtil.close(in);
            boolean delete = file.delete();
            if (delete) {
                System.out.println("删除临时二维码文件成功");
            } else {
                System.out.println("删除临时二维码文件失败");
            }
        }
        String objectURL = ossProperties.getAgency() + "/" + ossProperties.getBucketName() + "/" + id + "-prevention.jpg";
        preventionUnit.setZxingUrl(HexUtil.encodeHexStr(objectURL));
        preventionUnitMapper.update(preventionUnit);
        return preventionUnit;
    }

    /**
     * 修改数据
     *
     * @param preventionUnit 实例对象
     * @return 实例对象
     */
    @Override
    public PreventionUnit update(PreventionUnit preventionUnit) {
        this.preventionUnitMapper.update(preventionUnit);
        return this.queryById(preventionUnit.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.preventionUnitMapper.deleteById(id) > 0;
    }


    @Override
    public List<PreventionCheckTask> getAllTaskByUnitId(Integer id) {
        String nickName = SecurityUtils.getLoginUser().getUser().getNickName();
        List<PreventionCheckTask> preventionCheckTasks = preventionUnitMapper.getAllTaskByUnitId(id,nickName);
        for (PreventionCheckTask checkTask : preventionCheckTasks) {
            Integer checkTaskConfigId = checkTask.getCheckTaskConfigId();
            if (checkTaskConfigId == null) {
                continue;
            }
            PreventionCheckTaskConfigVO preventionCheckTaskConfigVO = new PreventionCheckTaskConfigVO();
            preventionCheckTaskConfigVO.setId(checkTaskConfigId);
            List<PreventionCheckTaskConfigVO> preventionCheckTaskConfigVOS = preventionCheckTaskConfigMapper.queryAllByLimit(preventionCheckTaskConfigVO);
            if (CollUtil.isNotEmpty(preventionCheckTaskConfigVOS)) {
                checkTask.setPreventionCheckTaskConfig(preventionCheckTaskConfigVOS.get(0));
            }
        }
        return preventionCheckTasks;
    }
}
